package com.hry.store.cms.controller;

import com.hry.store.cms.common.Const;
import com.hry.store.cms.common.ServerResponse;
import com.hry.store.cms.pojo.ProductColor;
import com.hry.store.cms.service.ColorService;
import com.hry.store.cms.service.FileService;
import com.hry.store.cms.service.HotProductService;
import com.hry.store.cms.service.SecondCategoryService;
import com.hry.store.cms.util.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ColorController {
    @Autowired
    private ColorService colorService;
    @Autowired
    private FileService fileService;
    @Autowired
    private SecondCategoryService secondCategoryService;
    @Autowired
    private HotProductService hotProductService;
    /**
     * 查询颜色分类
     *
     * @param secondCategoryId
     * @param versionId
     * @return
     */
    @RequestMapping("color/getBySecondCategoryId")
    @ResponseBody
    public ServerResponse getBySecondCategoryId(@RequestParam("secondCategoryId") Integer secondCategoryId, @RequestParam("versionId") Integer versionId) {
        List<ProductColor> productColors = colorService.getBySecondCategoryId(secondCategoryId, versionId);
        if (!CollectionUtils.isEmpty(productColors)) {
            return ServerResponse.createBySuccess("查询成功", productColors);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "查询失败");
    }

    /**
     * 添加颜色分类
     */
    @RequestMapping(value = "color/addColor",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addColor(@RequestParam(value = "file", required = false) MultipartFile[] file,

                                   @RequestParam(value = "hotFile", required = false) MultipartFile hotFile,
                                   HttpServletRequest request) {

        String colorName = request.getParameter("color_name_2");
        String colorCode = request.getParameter("color_code_2");
        String prodcutDesc = request.getParameter("prodcutDec");
        Integer  secondCategoryId =  Integer.parseInt(request.getParameter("secondCategory"));
        Integer versionId = Integer.parseInt(request.getParameter("selectVersion"));
        Integer quantity = Integer.parseInt(request.getParameter("quantity"));
        String path = request.getSession().getServletContext().getRealPath("upload");
        StringBuffer buffer = new StringBuffer();
        for (MultipartFile files : file) {
            String targetFileName = fileService.upload(files, path);
            String url = PropertiesUtil.getProperty("FTP_SERVER_HTTP_PREFIX") + targetFileName;
            buffer.append(url + ",");
        }
        int index = buffer.lastIndexOf(",");
        String urls = buffer.substring(0, index);
        // 添加商品描述到分类表中
        secondCategoryService.addProductDescBySecondCategoryId(secondCategoryId, prodcutDesc);
        int resultCount = colorService.addColor(secondCategoryId, versionId, colorCode, colorName, urls, quantity);
        if (hotFile != null) {
            String targetFileName = fileService.upload(hotFile, path);
            String url = PropertiesUtil.getProperty("FTP_SERVER_HTTP_PREFIX") + targetFileName;
            // 插入到轮播图数据库中
            hotProductService.addLunBoImage(secondCategoryId, url, prodcutDesc);

        }
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("添加成功", Const.SUCCESS);
        }
        return ServerResponse.createByErrorCodeMessage(Const.ERROR, "添加失败");
    }




}
